package com.enfi.api.modules.bz.service.impl;

import com.alibaba.fastjson.JSON;
import com.enfi.api.modules.bz.service.FlowService;
import com.immortal.httpclientutil.HttpClientUtil;
import com.immortal.httpclientutil.common.HttpConfig;
import com.immortal.httpclientutil.exception.HttpProcessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
@Slf4j
public class FlowServiceImpl implements FlowService {

    @Value("${heda.flow.url:http://122.225.71.14:8019}")
    private String baseUrl="http://122.225.71.14:8019";
    /**
     * 创建工单
     */
    public String createOrder(Map map){
        String key=(String)map.getOrDefault("key","");
        HttpConfig config = HttpConfig.custom()
                .url(baseUrl+"/bpm/customize-api/"+key+"/create-order")	          //设置请求的url
                .json(JSON.toJSONString(map))        //设置请求参数，没有则无需设置
                .encoding("utf-8");
        return resolveResponse(config);
    }

    public void before(Map map){
        if(!map.containsKey("index")){
            map.put("index",1);
        }
         map.put("index",Integer.valueOf(map.get("index").toString()));
        if(!map.containsKey("size")){
            map.put("size",10);
        }
        map.put("size",Integer.valueOf(map.get("size").toString()));
    }
    /**
     * 工单列表
     * @param map
     */
    public String  list(Map map){
        before(map);
        String state=(String)map.getOrDefault("state","all");
        HttpConfig config = HttpConfig.custom()
                .url(baseUrl+"/bpm/app/process-instance/list/"+state)	          //设置请求的url
                .json(JSON.toJSONString(map))        //设置请求参数，没有则无需设置
                .encoding("utf-8");
        return resolveResponse(config);
    }
    /**
     * 任务列表
     * @param map
     */
    public String taskList(Map map){
        before(map);
        HttpConfig config = HttpConfig.custom()
                .url(baseUrl+"/bpm/app/task/list")
                .json(JSON.toJSONString(map))        //设置请求参数，没有则无需设置
                .encoding("utf-8");
        return resolveResponse(config);
    }
    /**
     * 任务详情
     * @param map
     */
    public String detail(Map map){
        String id=(String)map.getOrDefault("id","");
        HttpConfig config = HttpConfig.custom()
                .url(baseUrl+"/bpm/app/task/form-definition")	          //设置请求的url
                .json(JSON.toJSONString(map))        //设置请求参数，没有则无需设置
                .encoding("utf-8");
        return resolveResponse(config);
    }

    /**
     * 任务详情
     * @param map
     */
    public String history(Map map){
        HttpConfig config = HttpConfig.custom()
                .url(baseUrl+"/bpm/app/process-instance/history")	          //设置请求的url
                .json(JSON.toJSONString(map))        //设置请求参数，没有则无需设置
                .encoding("utf-8");
        return resolveResponse(config);
    }
    /**
     * 任务流转
     * @param map
     */
    public String complete(Map map){
        String id=(String)map.getOrDefault("id","");
        HttpConfig config = HttpConfig.custom()
                .url(baseUrl+"/bpm/app/task/complete")	          //设置请求的url
                .json(JSON.toJSONString(map))        //设置请求参数，没有则无需设置
                .encoding("utf-8");
        return resolveResponse(config);
    }

    @Override
    public String transfer(Map map) {
        String taskId=(String)map.getOrDefault("taskId","");
        HttpConfig config = HttpConfig.custom()
                .url(baseUrl+"/bpm/customize-api/task/"+taskId+"/taskTransfer")	          //设置请求的url
                .json(JSON.toJSONString(map))        //设置请求参数，没有则无需设置
                .encoding("utf-8");
        return resolveResponse(config);
    }

    @Override
    public String suspended(Map map) {
        String id=(String)map.getOrDefault("id","");
        HttpConfig config = HttpConfig.custom()
                .url(baseUrl+"/bpm/app/process-instance/"+id+"/suspended")	          //设置请求的url
                .json(JSON.toJSONString(map))        //设置请求参数，没有则无需设置
                .encoding("utf-8");
        return resolveResponse(config);
    }
    @Override
    public String getAssign(Map map) {
        String taskId=(String)map.getOrDefault("instanceId","");
        HttpConfig config = HttpConfig.custom()
                .url(baseUrl+"/bpm/customize-api/task/"+taskId+"/getAssignees")	          //设置请求的url
                .json(JSON.toJSONString(map))        //设置请求参数，没有则无需设置
                .encoding("utf-8");
        return resolveResponse(config);
    }
    public String resolveResponse(HttpConfig config){
        try {
            String res= HttpClientUtil.post(config);
            return res;
        } catch (HttpProcessException e) {
           log.warn("flow error:{}",e.getMessage());
        }
        return "";
    }
    public String resolveGetResponse( HttpConfig config){
        try {
            String res= HttpClientUtil.get(config);
            return res;
        } catch (HttpProcessException e) {
            log.warn("flow error:{}",e.getMessage());
        }
        return "";
    }
}
